<?php

class Borrower extends Controller

{
	private $tbl = 'tbl_borrowers';
        private  $tbl_borrower_qty = 'tbl_borrower_quantities';

        private $tbl_member = 'tbl_members';

        function Borrower()

	{

		parent::Controller();

                $this->load->model('borrower_model');

                $this->load->helper('text');

	}

	function index()

	{

           	$data['total_borrower'] = $this->borrower_model->total_borrower();

                create_pagination($data['total_borrower'], 3, config_item('per_page'));

                $data['borrowers'] = $this->borrower_model->get_borrower(config_item('per_page'), $this->uri->segment(3));

		$data['page_title']	= 'Manage Publication Borrower';

		$data['page'] = 'borrower_list_v';

		$this->load->view('main', $data);	

	}

	

	function add()

	{

                $this->load->model('member_model');

		if($this->input->post('submit_borrower'))

		{

			$this->add_edit();	

		}

		$data['page_title']	 = 'Add New Borrower';

		$data['page'] = 'borrower_add_v';

                $data['member_list'] = $this->member_model->get_member_list();

		$this->load->view('main', $data);

	}

	

	function edit($publication_id)

	{

		if($this->input->post('submit_borrower'))

		{

			$this->add_edit($publication_id);

		}

		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'locations_name', 'parent_id = 0');

		$data['_borrower'] = $this->general_db_model->getById($this->tbl, 'publication_id', $publication_id);

		$data['page_title']	 = 'Update Publication Borrower';

		$data['page'] = 'borrower_add_v';

		$this->load->view('main', $data);	

	}

	

	function add_edit($id = NULL)

	{

            $user_type = $this->input->post('user_type');

            switch ($user_type){

                case "member":

                    $data['member_id'] = $this->input->post('member_id');

                    $member_detail = $this->general_db_model->getById($this->tbl_member, 'id', $data['member_id']);

                    $data['first_name'] = $member_detail->first_name;

                    $data['last_name'] = $member_detail->last_name;

                    $data['email'] = $member_detail->email;

                    $data['address1'] = $member_detail->address1;

                    $data['address2'] = $member_detail->address2;

                    $data['phone1'] = $member_detail->phone1;

                    $data['phone2'] = $member_detail->phone2;

                    break;

                default:

                    $data['member_id'] = 0;

                    $data['first_name'] = $this->input->post('first_name');

                    $data['last_name'] = $this->input->post('last_name');

                    $data['email'] = $this->input->post('email');

                    $data['address1'] = $this->input->post('address1');

                    $data['address2'] = $this->input->post('address2');

                    $data['phone1'] = $this->input->post('phone1');

                    $data['phone2'] = $this->input->post('phone2');

            }

            if($id)

            {

		 	$this->general_db_model->update($this->tbl, $data, 'id = '.$id);

                        $borrower_id = $id;

			$this->session->set_flashdata('success_message', "Borrower - Successfully updated.");

            }

            else

            {

		 	$this->general_db_model->insert($this->tbl, $data);

                        $borrower_id = $this->db->insert_id();

		 	$this->session->set_flashdata('success_message', "Borrower - Successfully added.");

            }

            $publication_list = $this->input->post('publication_list');

            $issue_date  = date('Y-m-d');

            $i=0;

            foreach($publication_list as $publication_id){

                $dataEnQuantity['issue_date'] = $issue_date;

                $dataEnQuantity['borrower_id'] = $borrower_id;

                $dataEnQuantity['publication_id'] = $publication_id;

                $dataEnQuantity['refundable_qty']=0;

                $dataEnQuantity['non_refundable_qty']=0;

                $dataEnQuantity['type']='en';

                $dataNpQuantity['issue_date'] = $issue_date;

                $dataNpQuantity['refundable_qty']=0;

                $dataNpQuantity['non_refundable_qty']=0;

                $dataNpQuantity['borrower_id'] = $borrower_id;

                $dataNpQuantity['publication_id'] = $publication_id;

                $dataNpQuantity['type'] = 'np';

                $cb_refundable=$this->input->post('cb_refundable');

                $cb_non_refundable=$this->input->post('cb_non_refundable');

                if($cb_refundable[$i]){

                    $refundable_en_days_list = $this->input->post('refundable_en_days');

                    $refundable_en_days = (is_numeric($refundable_en_days_list[$i]) && $refundable_en_days_list[$i]!="")?$refundable_en_days_list[$i]:15;

                    $return_date_format = mktime(0, 0, 0, date('m'), date('d')+$refundable_en_days, date('Y'));

                    $dataEnQuantity['return_date'] = date('Y-m-d',$return_date_format);

                    $refundable_np_quantity = $this->input->post('refundable_np_quantity');

                    $refundable_np_days_list = $this->input->post('refundable_np_days');

                    $refundable_np_days = (is_numeric($refundable_np_days_list[$i]) && $refundable_np_days_list[$i]!="")?$refundable_np_days_list[$i]:15;

                    $return_date_format = mktime(0, 0, 0, date('m'), date('d')+$refundable_np_days, date('Y'));

                    $dataNpQuantity['return_date'] = date('Y-m-d',$return_date_format);

                    $refundable_en_quantity = $this->input->post('refundable_en_quantity');

                    $refundable_np_quantity = $this->input->post('refundable_np_quantity');

                    $dataEnQuantity['refundable_qty'] = (is_numeric($refundable_en_quantity[$i]) && $refundable_en_quantity[$i]!="")?$refundable_en_quantity[$i]:0;

                    $dataNpQuantity['refundable_qty'] = (is_numeric($refundable_np_quantity[$i]) && $refundable_np_quantity[$i]!="")?$refundable_np_quantity[$i]:0;
					
					if($dataEnQuantity['refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'en', $dataEnQuantity['refundable_qty'], true);
						
					if($dataNpQuantity['refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'np', $dataNpQuantity['refundable_qty'], true);

                }

                if($cb_non_refundable[$i]){

                    $non_refundable_en_quantity = $this->input->post('non_refundable_en_quantity');

                    $non_refundable_np_quantity = $this->input->post('non_refundable_np_quantity');

                    $dataEnQuantity['return_date'] = "";

                    $dataNpQuantity['return_date'] = "";

                    $dataEnQuantity['non_refundable_qty'] = (is_numeric($non_refundable_en_quantity[$i]) && $non_refundable_en_quantity[$i]!="")?$non_refundable_en_quantity[$i]:0;

                    $dataNpQuantity['non_refundable_qty'] = (is_numeric($non_refundable_np_quantity[$i]) && $non_refundable_np_quantity[$i]!="")?$non_refundable_np_quantity[$i]:0;
					
					if($dataEnQuantity['non_refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'en', $dataEnQuantity['non_refundable_qty']);
						
					if($dataNpQuantity['non_refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'np', $dataNpQuantity['non_refundable_qty']);

                }
					
               // print_r($dataNpQuantity);exit;

                $this->general_db_model->insert($this->tbl_borrower_qty, $dataEnQuantity);

                $this->general_db_model->insert($this->tbl_borrower_qty, $dataNpQuantity);

                $i++;

            }

            redirect(current_url());

	}

	

	function delete()

	{

	 	$location_id = $this->input->post('c_id');

		$this->general_db_model->delete($this->tbl, 'id = '.$location_id);

	}

        function ajax_add_borrower_form(){

            $this->load->model('publication_model');

            $cntNo = $this->input->post('cntNo');

            $publication_list = $this->publication_model->get_publication_list();

            $data['cntNo']	 = $cntNo;

            $data['publication_list']	 = $publication_list;

            $this->load->view('borrower_ajax_add_borrower_form',$data);

        }

        function ajax_get_max_publication(){

            $this->load->model('quantity_model');

            $publication_id = $this->input->post('publication_id');

            $quantity_en_list = $this->quantity_model->get_quantity_by_pub_type($publication_id,'en');

            $quantity_np_list = $this->quantity_model->get_quantity_by_pub_type($publication_id,'np');

            echo "~~".$quantity_en_list->total_qty_distribution."~~".$quantity_np_list->total_qty_distribution."~~";



        }

}
?>